package com.pshcp.service;

import com.framework.base.PageList;
import com.framework.tool.CommonTool;
import com.framework.util.SystemUtil;
import com.github.pagehelper.PageHelper;
import com.pshcp.common.enums.CommonStatusEnum;
import com.pshcp.domain.dto.AppVersionDTO;
import com.pshcp.domain.model.AppVersion;
import com.pshcp.mapper.AppVersionMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * APP版本升级 服务实现类
 * </p>
 *
 * @author auto generator
 * @since 2022-11-21
 */
@Service
public class AppVersionService {

    @Autowired
    private AppVersionMapper appVersionMapper;

    public PageList<AppVersion> pageList(AppVersionDTO appVersionDTO){
        if(appVersionDTO.getPageNum() != null && appVersionDTO.getPageSize() != null){
            PageHelper.startPage(appVersionDTO.getPageNum(), appVersionDTO.getPageSize());
        }
        Example example = new Example(AppVersion.class);
        Example.Criteria criteria = example.createCriteria();
        if(appVersionDTO.getStatus() != null){
            criteria.andEqualTo("status", appVersionDTO.getStatus());
        }else{
            criteria.andEqualTo("status", CommonStatusEnum.INIT.getCode());
        }
        example.setOrderByClause(" created_time desc");
        List<AppVersion> list = appVersionMapper.selectByExample(example);
        return CommonTool.buildPageList(list);
    }

    public List<AppVersion> list(AppVersionDTO appVersionDTO){
        Example example = new Example(AppVersion.class);
        Example.Criteria criteria = example.createCriteria();
        if(appVersionDTO.getStatus() != null){
            criteria.andEqualTo("status", appVersionDTO.getStatus());
        }else{
            criteria.andEqualTo("status", CommonStatusEnum.INIT.getCode());
        }
        if(appVersionDTO.getVersionNo() != null){
            criteria.andGreaterThan("versionNo", appVersionDTO.getVersionNo());
        }
        if(appVersionDTO.getVersionShow() != null){
            criteria.andGreaterThan("versionShow", appVersionDTO.getVersionShow());
        }
        if(appVersionDTO.getAppType() != null){
            criteria.andEqualTo("appType", appVersionDTO.getAppType());
        }
        if(appVersionDTO.getOsType() != null){
            criteria.andEqualTo("osType", appVersionDTO.getOsType());
        }
        if(appVersionDTO.getPublish() != null){
            criteria.andEqualTo("publish", appVersionDTO.getPublish());
        }
        example.setOrderByClause(" version_no desc");
        return appVersionMapper.selectByExample(example);
    }

    public List<AppVersion> select(AppVersion appVersion){
        return appVersionMapper.select(appVersion);
    }

    public int insert(AppVersion appVersion){
        appVersion.setId(SystemUtil.getId());
        appVersion.setStatus(CommonStatusEnum.INIT.getCode());
        appVersion.setCreatedTime(new Date());
        return appVersionMapper.insert(appVersion);
    }

    public int update(AppVersion appVersion){
        appVersion.setUpdateTime(new Date());
        return appVersionMapper.updateByPrimaryKeySelective(appVersion);
    }

    public AppVersion findById(Long id){
        return appVersionMapper.selectByPrimaryKey(id);
    }

    public List<AppVersion> findByIds(List<Long> ids){
        Example example = new Example(AppVersion.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andIn("id", ids);
        return appVersionMapper.selectByExample(example);
    }

    public int delById(Long id){
        Example example = new Example(AppVersion.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("id", id);

        AppVersion appVersion = new AppVersion();
        appVersion.setStatus(CommonStatusEnum.DEL.getCode());
        return appVersionMapper.updateByExampleSelective(appVersion, example);
    }

}
